<?php

function skip_empty_array($array){
	$new_array=array();
	foreach($array as $_array){
		if($_array!=''){$new_array[]=$_array;}
	}
	return $new_array;
}
function user_layer_loop($uid,$t,$l,$config){
	$t->db->where('layer',1);
	$t->db->where('from_uid',$uid);
	$query=$t->db->get('user_layers');
	$user_layers=$query->result_array();
	$layer_html="";
	$i=1;
	$next_l=$l+1;
	foreach($user_layers as $user_layer){
		$u=$t->user_model->get_user($user_layer['uid']);
		$layer_html.="<table class='common-table ".($i==1?'':'margin-top-10')."'  cellspacing=0 cellpadding=0 style='width:100%;'>".
			"<tr class='header'>".
				"<td style='border-right:none;width:1%;position:relative;'><div style='position:absolute;top:50%;height:2px;width:15px;overflow:hidden;margin-left:-20px;background:Red;'></div><img src='".static_path(true)."images/avatar/".avatar($u['user_avatar'])."' style='width:24px;height:24px;'/></td><td style='width:1%;'><img src='".templates_path(true)."images/level".$u['user_level'].".png'/></td><td>".$l."层会员，".$user_layer['uid']."号会员，".$u['user_nickname']."，".$config['level_'.$u['user_level'].'_name']." ， 累积入局 ".$u['user_circle']." 次 ， 累积提供帮助".$u['user_sum_coins'].$config['coins_name']."</td>".

			"</tr>".
			"<tr>".
				"<td colspan='4' style='padding-left:13px;'>".
					user_layer_loop($user_layer['uid'],$t,$next_l,$config).
				"</td>".
			"</tr>".
		"</table>";
		$i++;
	}
	$html='';
	if($layer_html!=''){
		$html="<table class='tree-border-table common-table' cellspacing=0 cellpadding=0 style='width:100%;border-left:2px solid red;'>".
			"<tr>".
				"<td class='no-border-td' style='padding-left:15px;'>".
					$layer_html.
				"</td>".
			"</tr>".
		"</table>";
	}else{
		$html="<table class='no-border-table common-table' cellspacing=0 cellpadding=0 style='width:100%;'>".
			"<tr>".
				"<td class='no-border-td' ><span style='margin-left:-10px;'>无下级会员</span></td>".
			"</tr>".
		"</table>";
	}
	return $html;
}
function send_email($email,$type,$params){
	$CI=&get_instance();
	$config=$CI->system_model->get_config(false);
	require_once "api/email.class.php";
	$smtpserver=$config['smtp_host'];
	$smtpserverport=$config['smtp_port'];
	$smtpusermail=$config['smtp_email'];
	$smtpemailto=$email;
	$smtpuser=$config['smtp_user'];
	$smtppass=$config['smtp_pass'];
	$mailtype="HTML";
	if($type=='SAFE'){
		$mailtitle=$config['sitename'].'邮箱验证码';
		$mailcontent="<p>您的邮箱地址为".$email."</p><p>验证码为：</p><p><strong style='color:red;'>".$params['authcode']."</strong></p><p>请复制该验证码完成邮箱验证</p>";
	}
	if($type=='PROFILES'){
		$mailtitle=$config['sitename'].'邮箱验证码';
		$mailcontent="<p>修改个人资料需要验证您的安全邮箱</p><p>验证码为：</p><p><strong style='color:red;'>".$params['authcode']."</strong></p><p>请复制该验证码完成个人资料修改操作</p>";
	}
	if($type=='FINDPASSWORD'){
		$user=$params['user'];
		$mailtitle=$config['sitename'].'找回密码信件';
		$mailcontent="<p>您申请找回登录密码，请点击以下链接快速登录后修改密码</p><p><a href='".site_url('m/quicklogin/'.$user['uid'].'/'.md5($user['uid'].$user['user_safe_email'].$config['superpass']))."'>".site_url('m/quicklogin/'.$user['uid'].'/'.md5($user['uid'].$user['user_safe_email'].$config['superpass']))."</a></p>";
	}
	if($type=='ACTIVE'){
		$user=$params['user'];
		$mailtitle=$config['sitename'].'帐号激活信件';
		$mailcontent="<p>尊敬的会员您好</p><p>您申请成为".$config['sitename']."会员，现需要验证您的邮箱真实性以激活您的个人账户</p><p>请点击以下链接,或者将链接复制到浏览器中访问，用以激活您的账户</p><p><a href='".site_url('m/active/'.$user['uid'].'/'.md5($user['uid'].$user['user_email'].$config['superpass']))."'>".site_url('m/active/'.$user['uid'].'/'.md5($user['uid'].$user['user_email'].$config['superpass']))."</a></p>";
	}
	
	$smtp=new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);
	$smtp->debug=false;
	$state=$smtp->sendmail($smtpemailto,$smtpusermail,$mailtitle,$mailcontent,$mailtype);
}
function prefix($num,$len,$fix){
	$pre='';
	if(strlen($num)<=$len){
		$pre=str_repeat($fix,$len-strlen($num));
	}
	return $pre.''.$num;
}
function readale_price($price){
	if($price<1000){return intval($price);}
	if($price<10000){return intval($price/1000).'千';}
	if($price<100000){return intval($price/10000).'万';}
	if($price<1000000){return intval($price/10000).'万';}
	if($price<10000000){return intval($price/10000).'万';}
}
function get_lan(){
	$lan=get_cookie('lan');
	$lan=in_array($lan,array('en','zh'))?$lan:'zh';
	return $lan;
}
function lan($key){
	static $lans=array();
	if(count($lans)==0){
		$ci=&get_instance();
		$lans=$ci->system_model->get_total_lans(false);
	}
	$key=$key.'@'.get_lan();
	if(isset($lans[$key])){
		return $lans[$key];
	}else{
		return 'no lan';
	}
}
function formula_format($formula){
	$formula=preg_replace('/([0-9]+)/','<sub>$1</sub>',$formula);
	echo $formula;
}
function fetch_brackets($string){
	$reg='/\[([0-9]+)\]/';
	preg_match_all($reg,$string,$results);
	$array=array();
	for($i=0;$i<count($results[1]);$i++){
		$array[]=$results[1][$i];
	}
	return $array;
}
function stringsplit($string,$reg){
	$reg=$reg;
	preg_match_all($reg,$string,$results);
	return isset($results[1])?$results[1]:array();
}
function waterimg($filename,$config){
	if($config['water_position']=='none'){return false;}
	$contents=file_get_contents($filename);
	$watercontents=file_get_contents(static_path(false).'images/system/'.$config['water']);
	$watersize=getimagesize(static_path(false).'images/system/'.$config['water']);
	$sourcesize=getimagesize($filename);
	$sourceimg=imagecreatefromstring($contents);
	$waterimg=imagecreatefromstring($watercontents);
		if($config['water_position']=='left-top')
			imagecopy($sourceimg,$waterimg,10,10,0,0,$watersize[0],$watersize[1]);
		if($config['water_position']==2)
			imagecopy($sourceimg,$waterimg,($sourcesize[0]-$watersize[0])/2,10,0,0,$watersize[0],$watersize[1]);
		if($config['water_position']=='right-top')
			imagecopy($sourceimg,$waterimg,($sourcesize[0]-$watersize[0]-10),10,0,0,$watersize[0],$watersize[1]);
		if($config['water_position']==4)
			imagecopy($sourceimg,$waterimg,10,$sourcesize[1]/2,0,0,$watersize[0],$watersize[1]);
		if($config['water_position']=='center')
			imagecopy($sourceimg,$waterimg,($sourcesize[0]-$watersize[0])/2,$sourcesize[1]/2,0,0,$watersize[0],$watersize[1]);
		if($config['water_position']==6)
			imagecopy($sourceimg,$waterimg,($sourcesize[0]-$watersize[0]-10),$sourcesize[1]/2,0,0,$watersize[0],$watersize[1]);
		if($config['water_position']=='left-bottom')
			imagecopy($sourceimg,$waterimg,10,$sourcesize[1]-$watersize[1]-10,0,0,$watersize[0],$watersize[1]);
		if($config['water_position']==8)
			imagecopy($sourceimg,$waterimg,($sourcesize[0]-$watersize[0])/2,$sourcesize[1]-$watersize[1]-10,0,0,$watersize[0],$watersize[1]);
		if($config['water_position']=='right-bottom')
			imagecopy($sourceimg,$waterimg,($sourcesize[0]-$watersize[0]-10),$sourcesize[1]-$watersize[1]-10,0,0,$watersize[0],$watersize[1]);
	$ext=strtolower(end(explode('.',$filename)));
	$func='image'.($ext=='jpg'?'jpeg':$ext);
	$func($sourceimg,$filename);
	imagedestroy($sourceimg);
	imagedestroy($waterimg);
}
function img2thumb($src_img,$dst_img,$maxwidth,$maxheight){
	if(!is_file($src_img)){return false;}
	$ot=strtolower(end(explode('.',$dst_img)));
	$otfunc='image'.($ot=='jpg'?'jpeg':$ot);
	$srcinfo=getimagesize($src_img);
	$src_w=$srcinfo[0];
	$src_h=$srcinfo[1];
	$type=strtolower(substr(image_type_to_extension($srcinfo[2]),1));
	$createfun='imagecreatefrom'.($type=='jpg'?'jpeg':$type);
	$scale=max($src_w/$maxwidth,$src_h/$maxheight);
	$dst_h=$src_h/$scale;
	$dst_w=$src_w/$scale;
	$src=$createfun($src_img);
	$dst=imagecreatetruecolor($dst_w,$dst_h);
	if(function_exists('imagecopyresampled')){
		imagecopyresampled($dst,$src,0,0,0,0,$dst_w,$dst_h,$src_w,$src_h);
	}else{
		imagecopyresized($dst,$src,0,0,0,0,$dst_w,$dst_h,$src_w,$src_h);
	}
	$otfunc($dst,$dst_img);
	imagedestroy($dst);
	imagedestroy($src);
	return true;
}
function imgcut($src_img,$dst_img,$x,$y,$w,$h){
	if(!is_file($src_img)){return false;}
	$ot=strtolower(end(explode('.',$dst_img)));
	$otfunc='image'.($ot=='jpg'?'jpeg':$ot);
	$srcinfo=getimagesize($src_img);
	$type=strtolower(substr(image_type_to_extension($srcinfo[2]),1));
	$createfun='imagecreatefrom'.($type=='jpg'?'jpeg':$type);
	$src=$createfun($src_img);
	$dst=imagecreatetruecolor($w,$h);
	if(function_exists('imagecopyresampled')){
		imagecopyresampled($dst,$src,0,0,$x,$y,$w,$h,$w,$h);
	}else{
		imagecopyresized($dst,$src,0,0,$x,$y,$w,$h,$w,$h);
	}
	$otfunc($dst,$dst_img);
	imagedestroy($dst);
	imagedestroy($src);
	return true;
}
function check_username($username){
	$ban=array("~","`","!","@","#","$","%","^","&","*","(",")","_","+","=","-","{","}","[","]",";",":","\'","\"","<",">",",",".","?","/","|","\\"," ","！","￥","…","《","》","｛","｝","【","】","；","：","‘","“","’","”","？","、","，","·","　");
	foreach($ban as $_ban){
		if(strstr($username,$_ban)){
			return '用户名中不允许包含特殊符号';
		}
	}
	$reg="/[\x{4e00}-\x{9fa5}a-zA-Z0-9]+/u";
	if(!preg_match($reg,$username)){
		return '只能使用中文，英文，数字作为用户名';
	}
	if(mbstrlen($username)>12){
		return '用户名最长只能是12个英文子母或者6个汉子';
	}
	if(mbstrlen($username)<4){
		return '请至少输入2个汉字或者4个英文子母作为用户名';
	}
	return '';
}
function check_email($email){
	$pattern="/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i";
	if(preg_match( $pattern,$email)){
		return true;
	}else{
		return false;
	}
}
function check_nickname($nickname){
	$ban=array("~","`","!","@","#","$","%","^","&","*","(",")","_","+","=","-","{","}","[","]",";",":","\'","\"","<",">",",",".","?","/","|","\\"," ","！","￥","…","《","》","｛","｝","【","】","；","：","‘","“","’","”","？","、","，","·","　");
	foreach($ban as $_ban){
		if(strstr($nickname,$_ban)){
			return '昵称中不允许包含特殊符号';
		}
	}
	$reg="/[\x{4e00}-\x{9fa5}a-zA-Z]+/u";
	if(!preg_match($reg,$nickname)){
		return '只能使用中文，英文做为您的昵称';
	}
	if(mbstrlen($nickname)>12){
		return '昵称最长只能是12个英文子母或者6个汉字';
	}
	if(mbstrlen($nickname)<4){
		return '请至少输入2个汉字或者4个英文字母作为昵称';
	}
	return '';
}
function js_redirect($url){
	$url=site_url().$url;
	echo '<script>top.location.href="'.$url.'"</script>';
	exit;
}
function get_ip(){
	$ip=false;
	if(!empty($_SERVER["HTTP_CLIENT_IP"])){
		$ip = $_SERVER["HTTP_CLIENT_IP"];
	}
	if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
		$ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
		if ($ip){
			array_unshift($ips, $ip); $ip = FALSE;
		}
		for ($i = 0; $i < count($ips); $i++){
			if (!eregi ("^(10|172.16|192.168).", $ips[$i])){
				$ip = $ips[$i];
				break;
			}
		}
	}
	$ip=$ip ? $ip : $_SERVER['REMOTE_ADDR'];
	$ip=$ip=='::1'?'127.0.0.1':$ip;
	return $ip;
}
function alert($msg,$redirect){
	echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><script>alert("'.$msg.'");';
	if($redirect=='BACK')
		echo 'history.back();</script></head></html>';
	elseif($redirect=='CLOSE')
		echo 'window.close();</script></head></html>';
	else
		echo 'location.href="'.$redirect.'";</script></head></html>';
	exit;
}
function fetch_html($html){
	$array=array("\n","\t","\r");
	$html=str_replace($array,array('','',''),$html);
	return $html;
}
function code2array($code){
	if($code=='null')
		return array();
	else{
		$code=base64_decode($code);
		$code=@unserialize($code);
		foreach($code as &$value){
			$value=urldecode($value);
		}
		return $code;
	}
}
function array2code($array,$replace=array(),$reset=array()){
	if(((count($array)==0 and is_array($array)) || !is_array($array)) and count($replace)==0)
		return 'null';
	else{
		foreach($replace as $key=>$value){
			$array[$key]=urlencode($value);
		}
		foreach($reset as $key){
			unset($array[$key]);
		}
		$code=serialize($array);
		return base64_encode($code);
	}
}
function multi($allcount,$perpage,$page,$url,$pagechar='?'){
	if($allcount<=1)
		return '';
	$allpage=ceil($allcount/$perpage);
	if($allpage==1)
		return '';
	$multi="<div id='page'><div class='page'><a class='notd' href='".str_replace($pagechar,1,$url)."'><span>首页</span></a><a class='notd' href='".str_replace($pagechar,($page==1?1:($page-1)),$url)."'><span>上一页</span></a>";
	$skip=0;
	for($i=1;$i<=$allpage;$i++){
		if($i-1 <= 1 or abs($i-$page)<=1 or abs($i-$allpage)<=1){
			if($i==$page){
				$multi.="<a class='common cur' href='".str_replace($pagechar,$i,$url)."'><span>".$i."</span></a>";
				$skip=0;
			}
			else{
				$multi.="<a href='".str_replace($pagechar,$i,$url)."' class='common'><span>".$i."</span></a>";
			}
		}
		elseif($skip==0){
			$multi.="<a class='noborder' href='javascript:;'>... </a>";
			$skip=1;
		}
	}
	$multi=$multi."<a class='notd' href='".str_replace($pagechar,($page==$allpage?$allpage:($page+1)),$url)."'><span>下一页</span></a><a class='notd' href='".str_replace($pagechar,$allpage,$url)."'><span>末页</span></a><a class='noborder'><span>共 ".$allcount." 项 , ".$allpage." 页</span></a></div></div>";
	return $multi;
}
function m_multi($allcount,$perpage,$page,$url,$pagechar='?'){
	if($allcount<=1)
		return '';
	$allpage=ceil($allcount/$perpage);
	if($allpage==1)
		return '';
	$multi="<div id='page'><div class='page'><a class='pre' data-href='".str_replace($pagechar,($page==1?1:($page-1)),$url)."' data-anchor='1'><span>&lt;</span></a><a class='info'>".$page."/".$allpage."</a><a class='next' data-href='".str_replace($pagechar,($page==$allpage?$allpage:($page+1)),$url)."' data-anchor='1'><span>&gt;</span></a></div></div>";
	return $multi;
}
function strip_blank($array){
	$newarray=array();
	for($i=0;$i<count($array);$i++){
		if(strlen($array[$i])==0 || $array[$i]==''){
			
		}else{
			$newarray[]=$array[$i];
		}
	}
	return $newarray;
}
function js_strlen($string){
	$len=0;
	$skip=3;
	for($i=0;$i<strlen($string);){
		if(ord($string[$i])>127){
			$i+=$skip;
		}else{
			$i++;
		}
		$len++;
	}
	return $len;
}
function mbstrlen($string){
	$len=0;
	$skip=3;
	for($i=0;$i<strlen($string);){
		if(ord($string[$i])>127){
			$i+=$skip;
			$len+=2;
		}else{
			$len++;
			$i++;
		}
	}
	return $len;
}
function mbstrlen_array($string){
	$len_array=array(0=>0,1=>0);
	$skip=3;
	for($i=0;$i<strlen($string);){
		if(ord($string[$i])>127){
			$i+=$skip;
			$len_array[0]+=1;
		}else{
			$len_array[1]+=1;
			$i++;
		}
	}
	return $len;
}
//截取中文函数
function mbsubstr($string,$len,$suffix=''){
	//$string=strip_tags($string);
	$encode = 'UTF-8';
	$jump=$encode=='UTF-8'?3:2;
	$temp_string='';
	$offset=0;
	$short_string=0;
	for($i=0;$i<$len;$i++){
		if(strlen(substr($string, $offset, 1))==0){
			$short_string=1;
			break;
		}
		if(ord(substr($string, $offset, 1)) > 0xa0){
			$temp_string .= substr($string, $offset, $jump);
			$offset+=$jump;
		}
		else{
			$temp_string .= substr($string, $offset, 1);
			$offset++;
		}
	}
	if(!$short_string and $string!=$temp_string)
		$temp_string.=$suffix;
	return $temp_string;
}
function browinfo(){
	if(strpos($_SERVER['HTTP_USER_AGENT'], "MSIE 8.0")) {
	$visitor_browser = "Internet Explorer 8.0";
	} elseif(strpos($_SERVER['HTTP_USER_AGENT'], "MSIE 7.0")) {
	$visitor_browser = "Internet Explorer 7.0";
	} elseif(strpos($_SERVER['HTTP_USER_AGENT'], "MSIE 6.0")) {
	$visitor_browser = "Internet Explorer 6.0";
	} elseif(strpos($_SERVER['HTTP_USER_AGENT'], "MSIE 5.5")) {
	$visitor_browser = "Internet Explorer 5.5";
	} elseif(strpos($_SERVER['HTTP_USER_AGENT'], "MSIE 5.0")) {
	$visitor_browser = "Internet Explorer 5.0";
	} elseif(strpos($_SERVER['HTTP_USER_AGENT'], "MSIE 4.01")) {
	$visitor_browser = "Internet Explorer 4.01";
	} elseif(strpos($_SERVER['HTTP_USER_AGENT'], "NetCaptor")) {
	$visitor_browser = "NetCaptor";
	} elseif(strpos($_SERVER['HTTP_USER_AGENT'], "Netscape")) {
	$visitor_browser = "Netscape";
	} elseif(strpos($_SERVER['HTTP_USER_AGENT'], "Lynx")) {
	$visitor_browser = "Lynx";
	} elseif(strpos($_SERVER['HTTP_USER_AGENT'], "Opera")) {
	$visitor_browser = "Opera";
	} elseif(strpos($_SERVER['HTTP_USER_AGENT'], "Konqueror")) {
	$visitor_browser = "Konqueror";
	} elseif(strpos($_SERVER['HTTP_USER_AGENT'], "Mozilla/5.0")) {
	$visitor_browser = "Mozilla";
	} else {
	$visitor_browser = "others";
	}
	return $visitor_browser;
}

function get_file_datas($file){
	return explode("|",trim(str_replace(array("\r","\n","|||","||"),array('',"|","|","|"),file_get_contents($file)),'|'));
}

function datas_pack($array){
	$pack_string='';
	if(count($array)){
		foreach($array as $_array){
			$string='';
			foreach($_array as $key=>$value){
				$string.=",".$key.':'.$value;
			}
			$string=trim($string,',');
			$pack_string.="|".$string;
		}
		$pack_string=trim($pack_string,'|');
	}
	return $pack_string;
}

function datas_unpack($pack_string){
	$array='';
	if(strstr($pack_string,'|')){
		$pack_array=explode('|',$pack_string);
	}elseif($pack_string){
		$pack_array=array($pack_string);
	}else{
		$pack_array=array();
	}
	foreach($pack_array as $_pack_array){
		$_pack_array=explode(',',$_pack_array);
		$_array=array();
		foreach($_pack_array as $__pack_array){
			list($key,$value)=explode(':',$__pack_array);
			$_array[$key]=$value;
		}
		$array[]=$_array;
	}
	return $array;
}
function readabletime($time){
	$d=$h=$m=$s=0;
	if($time>=86400){
		$d=intval($time/86400);
		$time=$time-($d*86400);
	}
	if($time>=3600){
		$h=intval($time/3600);
		$time=$time-($h*3600);
	}
	if($time>=60){
		$m=intval($time/60);
		$time=$time-($m*60);
	}
	$s=$time;
	return ($d?($d.'天'):'').($h?($h.'小时'):'').($m?($m.'分'):'').($s?($s.'秒'):'');
}
function readabledate($time){
	if($time>=31536000){
		$y=intval($time/31536000);
		$time=$time-($y*31536000);
		$d=intval($time/2592000);
		$time=$time-($d*2592000);
		return $y.'年'.$m.'个月';
	}
	if($time>=2592000){
		$m=intval($time/2592000);
		$time=$time-($m*2592000);
		$d=intval($time/86400);
		$time=$time-($d*86400);
		return $m.'个月'.$d.'天';
	}
	if($time>=86400){
		$d=intval($time/86400);
		$time=$time-($d*86400);
		$h=intval($time/3600);
		$time=$time-($h*3600);
		return $d.'天'.$h.'小时';
	}
	if($time>=3600){
		$h=intval($time/3600);
		$time=$time-($h*3600);
		$s=intval($time/60);
		$time=$time-($s*60);
		return $h.'小时'.$s.'分';
	}
	if($time>=60){
		$m=intval($time/60);
		$time=$time-($m*60);
		$s=intval($time);
		return $m.'分'.$s.'秒';
	}
}
function readabletimeen($time){
	$time=intval($time);
	$d=$h=$m=$s=0;
	if($time>=86400){
		$d=intval($time/86400);
		$time=$time-($d*86400);
	}
	if($time>=3600){
		$h=intval($time/3600);
		$time=$time-($h*3600);
	}
	if($time>=60){
		$m=intval($time/60);
		$time=$time-($m*60);
	}
	$s=$time;
	return ($d?($d.'D'):'').($h?($h.'H'):'').($m?($m.'M'):'').($s?($s.'S'):'');
}
function array_sort($arr,$keys,$type='asc'){
	$keysvalue = $new_array = array();
	foreach ($arr as $k=>$v){
		$keysvalue[$k] = $v[$keys];
	}
	if($type == 'asc'){
		asort($keysvalue);
	}else{
		arsort($keysvalue);
	}
	reset($keysvalue);
	foreach ($keysvalue as $k=>$v){
		$new_array[$k] = $arr[$k];
	}
	return $new_array;
}
function std_class_object_to_array($stdclassobject){
	$array=array();
	$_array = is_object($stdclassobject) ? get_object_vars($stdclassobject) : $stdclassobject;
	$_array=is_array($_array)?$_array:array();

	foreach ($_array as $key => $value) {
		$value = (is_array($value) || is_object($value)) ? std_class_object_to_array($value) : $value;
		$array[$key] = $value;
	}

	return $array;
}
function my_fopen($url, $limit = 10485760 , $post = '', $cookie = '', $bysocket = false,$timeout=5,$agent="") {
	if(ini_get('allow_url_fopen') && !$bysocket && !$post) {
		$fp = @fopen($url, 'r');
		$s = $t = '';
		if($fp) {
			while ($t=@fread($fp,2048)) {
				$s.=$t;
			}
			fclose($fp);
		}
		if($s) {
			return $s;
		}
	}

	$return = '';
	$agent=$agent?$agent:"Mozilla/5.0 (compatible; Googlebot/2.1; +http:/"."/www.google.com/bot.html)";
	$matches = parse_url(trim($url));
	$host = $matches['host'];
	$script = $matches['path'].(isset($matches['query']) ? '?'.$matches['query'] : '').(isset($matches['fragment']) ? '#'.$matches['fragment'] : '');
	$script = $script ? $script : '/';
	$port = !empty($matches['port']) ? $matches['port'] : 80;
	if($post) {
		$out = "POST $script HTTP/1.1\r\n";
		$out .= "Accept: */"."*\r\n";
		$out .= "Referer: $url\r\n";
		$out .= "Accept-Language: zh-cn\r\n";
		$out .= "Content-Type: application/x-www-form-urlencoded\r\n";
		$out .= "Accept-Encoding: none\r\n";
		$out .= "User-Agent: $agent\r\n";
		$out .= "Host: $host\r\n";
		$out .= 'Content-Length: '.strlen($post)."\r\n";
		$out .= "Connection: Close\r\n";
		$out .= "Cache-Control: no-cache\r\n";
		$out .= "Cookie: $cookie\r\n\r\n";
		$out .= $post;
	} else {
		$out = "GET $script HTTP/1.1\r\n";
		$out .= "Accept: */"."*\r\n";
		$out .= "Referer: $url\r\n";
		$out .= "Accept-Language: zh-cn\r\n";
		$out .= "Accept-Encoding: none\r\n";
		$out .= "User-Agent: $agent\r\n";
		$out .= "Host: $host\r\n";
		$out .= "Connection: Close\r\n";
		$out .= "Cookie: $cookie\r\n\r\n";
	}

	$errno='';
	$errstr='';
	$fp = my_fsockopen($host, $port, $errno, $errstr, $timeout);
	if(!$fp) {
		return false;
	} else {
		fwrite($fp, $out);
		$return = '';
		while(!feof($fp) && $limit > -1) {
			$limit -= 8192;
			$return .= @fread($fp, 8192);
			if(!isset($status)) {
				preg_match("|^HTTP/[^\s]*\s(.*?)\s|",$return, $status);
				$status=$status[1];
				if($status!=200) {
					return false;
				}
			}
		}
		fclose($fp);
		preg_match("/^Location: ([^\r\n]+)/m",$return,$match);
		if(!empty($match[1]) && $location=$match[1]) {
			if(strpos($location,":/"."/")===false) {
				$location=dirname($url).'/'.$location;
			}
			$args=func_get_args();
			$args[0]=$location;
			return call_user_func_array("my_fopen",$args);
		}
		if(false!==($strpos = strpos($return, "\r\n\r\n"))) {
			$return = substr($return,$strpos);
			$return = preg_replace("~^\r\n\r\n(?:[\w\d]{1,8}\r\n)?~","",$return);
			if("\r\n\r\n"==substr($return,-4)) {
				$return = preg_replace("~(?:\r\n[\w\d]{1,8})?\r\n\r\n$~","",$return);
			}
		}
		if(empty($return)){
			$return=file_get_contents($url);
		}
		return $return;
	}
}
function my_fsockopen($hostname, $port, $errno, $errstr, $timeout) {
	$fp = false;
	if(function_exists('fsockopen')) {
		@$fp = fsockopen($hostname, $port, $errno, $errstr, $timeout);
	} elseif(function_exists('pfsockopen')) {
		@$fp = pfsockopen($hostname, $port, $errno, $errstr, $timeout);
	}
	return $fp;
}
function week_string($i){
	$weeks=array(
		1=>'一',
		2=>'二',
		3=>'三',
		4=>'四',
		5=>'五',
		6=>'六',
		7=>'日',
	);
	return $weeks[$i];
}
function fix_string($string,$count,$fixchar){
	if(strlen($string)<$count){
		$string=str_repeat($fixchar,$count-strlen($string)).$string;
	}
	return $string;
}
function output_csv($data,$file_name = 'export'){
	header('Content-Type: text/csv');
	$str=mb_convert_encoding($file_name,'gbk','utf-8');
	header('Content-Disposition: attachment;filename="' .$str . '.csv"');
	header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
	header('Expires:0');
	header('Pragma:public');
	$string='';
	foreach($data as $key=>$line){
		foreach($line as $char=>$item){
			$item=str_replace(",","，",$item);
			$string.=$item.",";
		}
		$string=trim($string,",")."\n";
	}

	$string=mb_convert_encoding($string,'gbk','utf-8');
	echo $string;
	die();
}
